<template>
	<z-nav-bar title="提问详情" transparentTitle="auto"></z-nav-bar>
	<view class="flex_sbc">
		<image :src="setting.oss_pub_domain + '/app-img/icon/icon-question.png'" class="icon-size uni-mr-20"></image>
		<view v-if="askDetails.isUp == 1" class="uni-color-main">已解答</view>
		<view v-else class="uni-orange">未解答</view>
	</view>
	<view class="common-box phenomenon">
		<view class="z-font-32">{{ askDetails.descriptionTagName }}</view>
		<view class="flex_fsc uni-mt-32">
			<view class="line"></view>
			<view class="z-font-32">提问描述</view>
		</view>
		<view class="color-585C66 uni-mt-8">{{ askDetails.description }}</view>
		<z-preview-media :srcStr="askDetails.material"></z-preview-media>
	</view>
	<view v-if="askDetails.result.desc" class="flex_sbc uni-mt-50">
		<view class="flex_fsc">
			<image :src="setting.oss_pub_domain + '/app-img/icon/icon-answer.png'" class="icon-size uni-mr-20"></image>
			<view class="z-font-32 text-w">教练解答</view>
		</view>
		<view v-if="startState == 1" class="edit-button" @click="askResolve">编辑</view>
	</view>
	<view v-if="askDetails.result && askDetails.result.desc" class="common-box phenomenon">
		<view class="color-585C66 uni-mt-8">{{ askDetails.result.desc }}</view>
		<z-preview-media v-if="askDetails.result.material" :srcStr="askDetails.result.material"></z-preview-media>
	</view>
	<view v-if="askDetails.schemeList && askDetails.schemeList.length" class="flex_sbc uni-mt-50">
		<view class="flex_fsc">
			<image :src="setting.oss_pub_domain + '/app-img/icon/icon-scheme.png'" class="icon-size uni-mr-20"></image>
			<view class="z-font-32 text-w">解决方案</view>
		</view>
	</view>
	<view v-if="askDetails.schemeList && askDetails.schemeList.length" class="common-box scheme">
		<view v-for="(item, index) in askDetails.schemeList">
			<view class="flex_sbc name">
				<view>{{ index + 1 }}.{{ item.name }}</view>
				<!-- <image :src="setting.oss_pub_domain + '/app-img/icon/icon_edit.png'" class="icon-size-32"></image> -->
			</view>
			<view class="flex_fsc uni-mt-32">
				<view class="line"></view>
				<view class="z-font-32">指导教学</view>
			</view>
			<view class="color-585C66 uni-mt-8">{{ item.description }}</view>
			<z-preview-media :srcStr="item.material"></z-preview-media>
			<!-- <view class="flex_fs uni-mt-32">
				<view class="line"></view>
				<view>
					<view class="z-font-32">训练要点</view>
					<view class="color-585C66">1. 注意胳膊</view>
					<view class="color-585C66">2. 注意手臂挥舞及形体转角的发力</view>
				</view>
			</view> -->
		</view>
	</view>
	<bottom-box v-if="startState == 1" :buts="buts" @change="butChange"></bottom-box>
</template>

<script setup>
import { getSchemeMaynDetail } from '@/api/scheme.js';
import { getPlayerDetial, questionToQuestion } from '@/api/player.js'
import { onShow } from '@dcloudio/uni-app';
import { ref, reactive, getCurrentInstance } from 'vue';
const { proxy: that } = getCurrentInstance();
const eventChannel = that.getOpenerEventChannel()

const askDetails = reactive(uni.getStorageSync('ask-details'));
askDetails.result = askDetails.result ? (typeof askDetails.result === 'object' ? askDetails.result : JSON.parse(askDetails.result)) : {schemeIdList: []}
const buts = reactive([
	{
		label: '添加为学员问题',
		bgColor: 'uni-color-main-bg',
		width: 420
	}
]),
startState = uni.getStorageSync('start-state');
console.log(askDetails);

getSchemeList()
getPlayerDetails()
if (askDetails.isUp == 0 && buts.length === 1) {
	setTimeout(() => {
		buts.unshift({
			label: '解答',
			bgColor: 'uni-orange-bg',
			width: 180
		})
	})
	
}
onShow(() => {
	if (buts.length === 2 && buts.length === 1) buts.shift()
})

function getPlayerDetails() {
	const player = uni.getStorageSync('player-info') || {}
	if (player.id == askDetails.relStudent) return
	getPlayerDetial(askDetails.relStudent).then(res => {
		uni.setStorageSync('player-info', res)
		console.log(res);
	})
}



function askResolve() {
	uni.setStorageSync('ask-details', askDetails)
	uni.navigateTo({
		url: '/pages/ask-questions/answer',
		events: {
			resolveDone(e) {
				Object.assign(askDetails, e)
				if (buts.length === 2) buts.splice(0, 1)
				askDetails.isUp = 1
				eventChannel.emit('resolveDone')
			}
		}
	});
}

function butChange(e) {
	if (buts.length === 2 && e === 0) askResolve()
	 else {
		 console.log(askDetails);
		questionToQuestion(askDetails.id).then(res => {
			that.tools.alert('添加成功')
			setTimeout(() => {
				uni.navigateBack()
			}, 1000)
		})
	 }
}

function getSchemeList() {
	if (!askDetails.result.schemeIdList || !askDetails.result.schemeIdList.length) return
	getSchemeMaynDetail(askDetails.result.schemeIdList.join(',')).then(res => {
		askDetails.schemeList = res
	})
}
</script>

<style lang="scss">
@import 'details.scss';
</style>
